package com.example.demo;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.google.common.collect.Lists;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest(classes={CourseYS.class})
public class CourseYS {


    public static void main(String[] args) {

//        String excelPath = "E:\\远教工作\\职业教育项目\\审核验收\\v2\\领航个人中心课程配置_单点登录.xls";
        String excelPath = "E:\\ttt\\tmp\\教务平台调整课程版本\\创锐.xlsx";
        read(excelPath);


    }


    private static  void read(String filePath){
        ExcelReader reader = ExcelUtil.getReader(filePath);

        List<Map<String,Object>> readAll = reader.readAll();
        StringBuilder sb = new StringBuilder();

        readAll.forEach(m->
                {
                    String courseName = Objects.toString(m.get("course_name"));
                    String courseid = Objects.toString(m.get("course_id"));
                    String oldcourseId = Objects.toString(m.get("old_course_id"));
                    sb.append(courseTpl(courseName,courseid,oldcourseId)).append("\n\r");



//                    int index = readAll.indexOf(m);
//                    String courseName = Objects.toString(m.get("course_name"));
//                    String courseHour = Objects.toString(m.get("course_hour"),"");
//                    sb.append(templateStudy(index,courseName,courseHour)).append("\n\r");

//                    String chooseId = Objects.toString(m.get("CHOOSE_ID"));
//                    sb.append(templateChooseId(chooseId)).append("\n\r");
                }
        );

//        FileWriter writer = new FileWriter("D:\\Temp\\yscourse.txt");
//        FileWriter writer = new FileWriter("D:\\Temp\\yscourse_mobile.txt");
//        FileWriter writer = new FileWriter("D:\\Temp\\yscourse_studyDetail.txt");
        FileWriter writer = new FileWriter("E:\\ttt\\tmp\\教务平台调整课程版本\\course_创锐.txt");
        writer.write(sb.toString());
    }


    private static  String courseTpl(String courseName,String courseId,String oldcourseId){
        String template ="INSERT INTO WORKPROCOURSE (WORKPROCOURSEID,WORKPROCOURSENAME,COURSEAPPID,SYNCHROFLAG,SYNCHROPLATFORM,VALIDFLAG,DELFLAG,CREATOR,CREATEDATE,IS_OPEN,COURSE_TYPE,LEARN_TYPE,PARENT_ID,COURSE_PASSING_SCORE,COURSE_PASSING_MUSTACT ) " +
                "VALUES ('"+courseId+"','"+courseName+"','APP119',1,   2,1,    0,'admin',SYSDATE, 1, 1, 1,'"+oldcourseId+"','60','999' );";
        return template;
    }

    private static  String template(String courseName,String courseParam){
        String template = "\n" +
                "<div class=\"review_main\">\n" +
                "    <div class=\"right_layout\">\n" +
                "        <div class=\"side mt40\">       \n" +
                "            <input  type=\"button\"  onclick=\"_loginToCourse('"+courseParam+"')\"\n" +
                "            class=\"continueInput staic_btn_style01 right10 loginToCourse\" value=\"继续学习\" >\n" +
                "        </div>\n" +
                "        <div class=\"main f12\">\n" +
                "            <h4 class=\"f18 blue_165690 yahei bottom10\">"+courseName+"</h4>\n" +
                "            <p class=\"bottom20\">学习有效时间：\n" +
                "                <span class=\"blue_7292b5 right5\">\n" +
                "                   2020-03-10</span>至\n" +
                "                <span class=\"blue_7292b5 left5\">\n" +
                "                  2021-03-10\n" +
                "                  </span>\n" +
                "            </p>\n" +
                "            <h3 class=\"font14 orange bottom10\">\n" +
                "                <div class=\"arrange_progress clearfix\">\n" +
                "                    <span class=\"fl right20\">当前成绩：</span><span>0分</span>\n" +
                "                </div>\n" +
                "                <div class=\"arrange_progress clearfix\">\n" +
                "                  <span class=\"fl right20\">当前进度：</span>\n" +
                "                    <div class=\"progress fl top5\">\n" +
                "                          <i class=\"progressPro green\" style=\"width:0.0%\" ></i>\n" +
                "                      <span class=\"progressProNum \" >0.0%</span>\n" +
                "                    </div>\n" +
                "               </div>\n" +
                "            </h3>          \n" +
                "        </div>\n" +
                "    </div>\n" +
                "</div>";
        return template;
    }


    private static  String templateMobile(int index,String courseName,String termcourseId,String courseId,String classId,String chooseId){
        String template = "\n"+
                "\t\t  final Map<String,String> m"+index+" = new HashMap<String,String>();\n" +
                "\t\t  m"+index+".put(\"id\", \""+termcourseId+"\");//TERMCOURSE_ID\n" +
                "\t\t  m"+index+".put(\"courseId\", \""+courseId+"\");\n" +
                "\t\t  m"+index+".put(\"classId\", \""+classId+"\");\n" +
                "\t\t  m"+index+".put(\"chooseId\", \""+chooseId+"\");\n" +
                "\t\t  m"+index+".put(\"name\", \""+courseName+"\");\n" +
                "\t\t  m"+index+".put(\"progress\", \"0\");\n" +
                "\t\t  m"+index+".put(\"beginTime\", \"2018-10-10\");\n" +
                "\t\t  m"+index+".put(\"endTime\", \"2023-10-10\");\n" +
                "\t\t  m"+index+".put(\"status\", \"2\");\n" +
                "\t\t  m"+index+".put(\"synchroplatform\", \"2\");\n" +
                "\t\t  courseList.add(m"+index+");";
        return template;
    }

    private static  String templateStudy(int index,String courseName,String studyHour){
        String template = "\t\t\t\t\t\t\t\t\t\t\t\t\t <li>\n" +
                "\t\t\t\t\t\t\t\t\t\t\t\t\t<div style=\"width:200px;\" >"+courseName+"</div>\n" +
                "\t\t\t\t\t\t\t\t\t\t\t\t\t<div>"+studyHour+"</div>\n" +
                "\t\t\t\t\t\t\t\t\t\t\t\t\t<div>未完成</div>\n" +
                "\t\t\t\t\t\t\t\t\t\t\t\t\t<div>2020-05-09至2022-06-30</div>\n" +
                "\t\t\t\t\t\t\t\t\t\t\t\t</li>";

        return template;
    }

    private static  String templateChooseId(String chooseId){
        String template = "chooseIdList.add(\""+chooseId+"\");";

        return template;
    }

    public static byte[] desEncrypt(byte[] data, String key, String charset) {
        try {
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            byte[] k = charset == null || charset.trim().isEmpty() ? key.getBytes() : key.getBytes(charset);
            SecretKey secretKey = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(k));
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(k));
            return cipher.doFinal(data);
        } catch (Exception e) {
            return null;
        }
    }
}
